package com.sunny.design.strategy.exam2;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * 策略模式获取不同公司的支付类
 *
 * @author Sunny
 * @date 2020/1/4 22:28
 */
public class PayStrategy {
    public static final String ALI_PAY = "AliPay";
    public static final String UNION_PAY = "UnionPay";
    public static final String WECHAT_PAY = "WechatPay";
    public static final String DEFAULT_PAY = ALI_PAY;
    private static Map<String, Payment> PAY_STRATEGY = Maps.newConcurrentMap();

    static {
        PAY_STRATEGY.put(ALI_PAY,new AliPay());
        PAY_STRATEGY.put(WECHAT_PAY,new WeChatPay());
        PAY_STRATEGY.put(UNION_PAY,new UnionPay());
    }

    /**
     * 获取支付类
     *
     * @param payKey 支付key
     */
    public static Payment getPayment(String payKey) {
        if (!PAY_STRATEGY.containsKey(payKey)) {
            return PAY_STRATEGY.get(DEFAULT_PAY);
        }
        return PAY_STRATEGY.get(payKey);
    }
}
